package com.pureut.production.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pureut.common.core.exception.GlobalException;
import com.pureut.common.core.utils.bean.BeanUtils;
import com.pureut.common.core.web.domain.AjaxResult;
import com.pureut.common.security.utils.SecurityUtils;
import com.pureut.production.domain.MasterRollSplicing;
import com.pureut.production.domain.dto.CutOrdersDocumentDetailDto;
import com.pureut.production.domain.dto.CutOrdersDocumentDto;
import com.pureut.production.domain.dto.MasterRollSplicingDto;
import com.pureut.production.domain.dto.MasterVolumeFeignDto;
import com.pureut.production.mapper.CutOrdersDocumentDetailMapper;
import com.pureut.production.mapper.CutOrdersDocumentMapper;
import com.pureut.production.mapper.MasterRollSplicingMapper;
import com.pureut.production.mapper.SpunlaceOutputDetailMapper;
import com.pureut.production.service.IMasterRollSplicingService;
import com.pureut.production.domain.vo.MasterRollSplicingVo;
import com.pureut.system.api.StorageFegin;
import com.pureut.system.api.vo.WmsStockInfoVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @Author: YSL
 * @Date: 2023-02-09 11:25:57
 * @Desc: 母卷拼接
 */
@Service("masterRollSplicingService")
public class MasterRollSplicingServiceImpl extends ServiceImpl<MasterRollSplicingMapper, MasterRollSplicing> implements IMasterRollSplicingService {

    /**
     * 仓库feign
     */
    @Resource
    StorageFegin storageFegin;

    /**
     * 母卷拼接mapper
     */
    @Resource
    MasterRollSplicingMapper masterRollSplicingMapper;

    /**
     * 分切制令单
     */
    @Resource
    private CutOrdersDocumentMapper cutOrdersDocumentMapper;

    /**
     * 水刺产出明细
     */
    @Resource
    private SpunlaceOutputDetailMapper spunlaceOutputDetailMapper;

    /**
     * 分期制令单明细
     */
    @Resource
    private CutOrdersDocumentDetailMapper cutOrdersDocumentDetailMapper;

    /**
     * 查询母卷拼接列表
     *
     * @param masterRollSplicingVo
     * @return
     */
    @Override
    public List<MasterRollSplicingDto> selectMasterRollList(MasterRollSplicingVo masterRollSplicingVo) {
        return masterRollSplicingMapper.selectMasterRollList(masterRollSplicingVo);
    }

    /**
     * 新增母卷拼接
     *
     * @param masterRollSplicingVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean add(MasterRollSplicingVo masterRollSplicingVo) {

        if (Objects.equals(masterRollSplicingVo.getExtensionMasterVolumeNumber(), masterRollSplicingVo.getTruncationMasterVolumeNumber())){
            throw new GlobalException("同一母卷无法进行拼接");
        }

        // 根据输入的接长母卷号码查询相关内容
        AjaxResult extension = storageFegin.selectMasterVolumeList(masterRollSplicingVo.getExtensionMasterVolumeNumber());
        MasterVolumeFeignDto data = JSON.parseObject(JSON.toJSONString(extension.get("data")), MasterVolumeFeignDto.class);

        // 判断母卷是否分切
        CutOrdersDocumentDetailDto cutOrdersDocumentInfo = cutOrdersDocumentDetailMapper.getCutOrdersDocumentInfo(data.getWsiMaterialNumber());
        if (!cutOrdersDocumentInfo.getSlittingStatus().equals("1")){
            throw new GlobalException("此接长母卷已分切,不可变更");
        }

        MasterRollSplicing masterRollSplicing = new MasterRollSplicing();
        BeanUtils.copyProperties(masterRollSplicingVo, masterRollSplicing);
        // 接长母卷产品编码
        masterRollSplicing.setMaterialCode(data.getWsiMaterialCode());
        // 接长母卷产品名称
        masterRollSplicing.setMaterialName(data.getWsiMaterialName());
        // 接长母卷产品规格
        masterRollSplicing.setMaterialSpec(data.getWsiMaterialSpec());
        // 接长母卷号码
        masterRollSplicing.setExtensionMasterVolumeNumber(data.getWsiMaterialNumber());
        // 原卷长
        masterRollSplicing.setExtensionOriginalRollLength(data.getWsiLength());
        // 新卷长 (新卷长=原卷长（接长母卷）+ 转移长度)
        masterRollSplicing.setExtensionNewRollLength(masterRollSplicingVo.getExtensionNewRollLength());
        // TODO 分切执行单
         masterRollSplicing.setConnectionId(cutOrdersDocumentInfo.getDocumentNum());
        // 部门id
        masterRollSplicing.setDeptId(SecurityUtils.getDeptId());
        // 变更人
        masterRollSplicing.setCreateBy(SecurityUtils.getLoginUser().getUsername());
        // 变更时间
        masterRollSplicing.setCreateTime(new Date());


        // 根据输入的截短母卷号码查询相关内容
        AjaxResult truncation = storageFegin.selectMasterVolumeList(masterRollSplicingVo.getTruncationMasterVolumeNumber());
        MasterVolumeFeignDto truncationData = JSON.parseObject(JSON.toJSONString(truncation.get("data")), MasterVolumeFeignDto.class);

        // 判断母卷是否分切
        CutOrdersDocumentDetailDto cutOrdersDocumentInfo1 = cutOrdersDocumentDetailMapper.getCutOrdersDocumentInfo(data.getWsiMaterialNumber());
        if (!cutOrdersDocumentInfo1.getSlittingStatus().equals("1")){
            throw new GlobalException("此截短母卷已分切,不可变更");
        }

        // 截短母卷号码
        masterRollSplicing.setTruncationMasterVolumeNumber(truncationData.getWsiMaterialNumber());
        // 原卷长
        masterRollSplicing.setTruncationOriginalRollLength(truncationData.getWsiLength());
        // 新卷长 (新卷长=原卷长（接长母卷）- 转移长度)
        masterRollSplicing.setTruncationNewRollLength(masterRollSplicingVo.getTruncationNewRollLength());
        // 转移长度
        masterRollSplicing.setTransferLength(masterRollSplicingVo.getTransferLength());

        // 如果转移长度大于截短母卷的长度则提示
        if (masterRollSplicing.getTransferLength() > masterRollSplicing.getTruncationOriginalRollLength()) {
            throw new GlobalException("转移长度大于截短母卷的原卷长,请正确输入");
        }
        // 接长的母卷与截短的母卷，产品编码相同才录入成功
        if (!Objects.equals(data.getWsiMaterialCode(), truncationData.getWsiMaterialCode())){
            throw new GlobalException("母卷不同无法进行拼接");
        }

        boolean save = save(masterRollSplicing);

        if (save) {
            // 如果截短新卷长大于0,则修改仓库信息
            if (masterRollSplicing.getTruncationNewRollLength() > 0) {
                // 修改接长母卷
                WmsStockInfoVo wmsStockInfoVo = new WmsStockInfoVo();
                // 接长母卷号码
                wmsStockInfoVo.setWsiMaterialNumber(masterRollSplicing.getExtensionMasterVolumeNumber());
                // 接长新卷长
                wmsStockInfoVo.setWsiLength(masterRollSplicing.getExtensionNewRollLength());
                AjaxResult ajaxResult = storageFegin.updateStockInfoById(wmsStockInfoVo);
                String dataMsg = JSON.parseObject(JSON.toJSONString(ajaxResult.get("msg")), String.class);

                // 修改水刺产出明细卷长(接长母卷)
                spunlaceOutputDetailMapper.editRollLengthChange(masterRollSplicing.getExtensionMasterVolumeNumber(), masterRollSplicing.getExtensionNewRollLength());

                // 修改分期制令单明细卷长
                cutOrdersDocumentDetailMapper.editCutOrdersRollLengthChange(masterRollSplicing.getExtensionMasterVolumeNumber(), masterRollSplicing.getExtensionNewRollLength());

                if ("操作成功".equals(dataMsg)) {
                    // 修改截短母卷
                    WmsStockInfoVo wmsStockInfoVo1 = new WmsStockInfoVo();
                    // 截短母卷号码
                    wmsStockInfoVo1.setWsiMaterialNumber(masterRollSplicing.getTruncationMasterVolumeNumber());
                    // 截短新卷长
                    wmsStockInfoVo1.setWsiLength(masterRollSplicing.getTruncationNewRollLength());
                    storageFegin.updateStockInfoById(wmsStockInfoVo1);

                    // 修改水刺产出明细卷长(截短母卷)
                    spunlaceOutputDetailMapper.editRollLengthChange(masterRollSplicing.getTruncationMasterVolumeNumber(), masterRollSplicing.getTruncationNewRollLength());

                    // 修改分期制令单明细卷长
                    cutOrdersDocumentDetailMapper.editCutOrdersRollLengthChange(masterRollSplicing.getTruncationMasterVolumeNumber(), masterRollSplicing.getTruncationNewRollLength());

                }
                // 如果新卷长为0,则在库存表中删除该母卷
            } else if (masterRollSplicing.getTruncationNewRollLength() == 0) {
                // 删除母卷
                String truncationMasterVolumeNumber = masterRollSplicing.getTruncationMasterVolumeNumber();
                storageFegin.remove(truncationMasterVolumeNumber);

                // 修改接长母卷
                WmsStockInfoVo wmsStockInfoVo = new WmsStockInfoVo();
                // 接长母卷号码
                wmsStockInfoVo.setWsiMaterialNumber(masterRollSplicing.getExtensionMasterVolumeNumber());
                // 接长新卷长
                wmsStockInfoVo.setWsiLength(masterRollSplicing.getExtensionNewRollLength());
                storageFegin.updateStockInfoById(wmsStockInfoVo);

                // 修改水刺产出明细卷长(接长母卷)
                spunlaceOutputDetailMapper.editRollLengthChange(masterRollSplicing.getExtensionMasterVolumeNumber(), masterRollSplicing.getExtensionNewRollLength());

                // 修改分期制令单明细卷长
                cutOrdersDocumentDetailMapper.editCutOrdersRollLengthChange(masterRollSplicing.getExtensionMasterVolumeNumber(), masterRollSplicing.getExtensionNewRollLength());


                // 根据母卷号码删除水刺产出明细
                spunlaceOutputDetailMapper.deleteSpunlaceOutputInfoById(truncationMasterVolumeNumber);

                // 根据母卷号码删除制令单明细
                cutOrdersDocumentDetailMapper.deleteCutOrdersInfoById(truncationMasterVolumeNumber);
           }
        }
        return save;
    }
}
